//
//  WHDiscoverSearch.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/6.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit

class WHDiscoverSearch: UIView {
    @IBOutlet weak var textView: UITextField!

    @IBOutlet weak var textViewTrailing: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    override func awakeFromNib() {
        
    
        self.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, self.bounds.height)
        
        self.textView.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        self.textView.leftView?.frame = CGRectMake(0, 0, self.textView.bounds.height, self.textView.bounds.height)
        
        //设置图片显示模式
        self.textView.leftView?.contentMode = .Center
        
        //设置图片显示
        self.textView.leftViewMode = .Always
        
        //设置圆角
//        self.textView.layer.cornerRadius = 20
//        self.textView.layer.masksToBounds = true
        
    }
    
    
    @IBAction func textDidBegin(sender: UITextField) {
       
        //设置textView长度变小
        self.textViewTrailing.constant = self.cancelBtn.bounds.width
        
        //设置动画
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textView.layoutIfNeeded()
        }
        
    }
    
    @IBAction func btnDidClick(sender: UIButton) {
        
        self.textViewTrailing.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textView.layoutIfNeeded()
        }
        
        textView.resignFirstResponder()
    }
    
    
    
    class func loadNib() -> WHDiscoverSearch{
        let textView = NSBundle.mainBundle().loadNibNamed("WHTextField", owner: nil, options: nil).last as! WHDiscoverSearch
        
        return textView
        
    }

}
